/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.sgt;

import com.max.oti.system.model.AlumnRequirement;
import com.max.oti.system.model.Requirement;
import com.max.oti.system.service.interfac.IAlumnRequirementService;
import com.max.oti.system.service.interfac.IRequirementService;
import com.max.oti.system.util.FacesUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.primefaces.event.TransferEvent;
import org.primefaces.model.DualListModel;

/**
 *
 * @author max
 */
@ManagedBean
@ViewScoped
public class BachillorBean {

    @ManagedProperty("#{requirementService}")
    IRequirementService requirementService;
    @ManagedProperty("#{alumnRequirementService}")
    IAlumnRequirementService alumnRequirementService;
    private List<Requirement> listRequirement;
    ////////////////////////////////////////////////////////
    private DualListModel<Requirement> dualListModelRequirement;
    //private List<Requirement> listConververRequiremt;
    ////////////////////////////////////////////////////////
    private List<AlumnRequirement> listAlumnRequirement;
    private boolean existAlumn = false;
    private String codeAlumn;

    @PostConstruct
    public void initCollections() {
        try {
            listRequirement = requirementService.listHQL("FROM Requirement r");
            dualListModelRequirement = new DualListModel<Requirement>(listRequirement, new ArrayList<Requirement>());

        } catch (Exception ex) {
            Logger.getLogger(BachillorBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void saveAlumnRequirement() {
        System.out.println("Source");
        for (int i = 0; i < dualListModelRequirement.getSource().size(); i++) {
            System.out.println(dualListModelRequirement.getSource().get(i).getName());
        }
        System.out.println("Target");
        for (int i = 0; i < dualListModelRequirement.getTarget().size(); i++) {
            System.out.println(dualListModelRequirement.getTarget().get(i).getName());
            AlumnRequirement ar = new AlumnRequirement();
            ar.setCodeAlumn(codeAlumn);
            ar.setIdRequirement(dualListModelRequirement.getTarget().get(i));
            try {
                alumnRequirementService.save(ar);
            } catch (Exception ex) {
                Logger.getLogger(BachillorBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }

    public void onTransfer(TransferEvent event) {
        
    }

    public void searchAlumn() {
        //FacesUtil.addMessage("Falta integrar un WEB-SERVICE, pero puede guardar sus datos.", null, FacesMessage.SEVERITY_WARN);
        if (codeAlumn.startsWith("00") && codeAlumn.length() == 10) {

            String HQL = "from AlumnRequirement ar where ar.codeAlumn='(codeAlumn)'";
            HQL = HQL.replace("(codeAlumn)", codeAlumn);
            try {
                List<AlumnRequirement> listRequirementTemp = alumnRequirementService.listHQL(HQL);
                dualListModelRequirement.setTarget(new ArrayList<Requirement>());
                for (int i = 0; i < listRequirementTemp.size(); i++) {
                    listRequirement.remove(listRequirementTemp.get(i).getIdRequirement());
                    dualListModelRequirement.getTarget().add(listRequirementTemp.get(i).getIdRequirement());
                }
                if (listRequirement.isEmpty()) {
                    FacesUtil.addMessage("El Alumno Cumple con todos los requisitos", null, FacesMessage.SEVERITY_INFO);
                    existAlumn = false;
                } else {
                    dualListModelRequirement.setSource(listRequirement);
                    FacesUtil.addMessage("Cargado los Requerimientos del alumno: " + codeAlumn, null, FacesMessage.SEVERITY_INFO);
                    existAlumn = true;
                }

            } catch (Exception ex) {
                existAlumn = false;
                Logger.getLogger(BachillorBean.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            FacesUtil.addMessage("Debe ingresar un código Válido", null, FacesMessage.SEVERITY_WARN);
            existAlumn = false;
        }

    }

    public void setRequirementService(IRequirementService requirementService) {
        this.requirementService = requirementService;
    }

    public List<Requirement> getListRequirement() {
        return listRequirement;
    }

    public void setListRequirement(List<Requirement> listRequirement) {
        this.listRequirement = listRequirement;
    }

    public DualListModel<Requirement> getDualListModelRequirement() {
        return dualListModelRequirement;
    }

    public void setDualListModelRequirement(DualListModel<Requirement> dualListModelRequirement) {
        this.dualListModelRequirement = dualListModelRequirement;
    }

    public boolean isExistAlumn() {
        return existAlumn;
    }

    public void setExistAlumn(boolean existAlumn) {
        this.existAlumn = existAlumn;
    }

    public String getCodeAlumn() {
        return codeAlumn;
    }

    public void setCodeAlumn(String codeAlumn) {
        this.codeAlumn = codeAlumn;
    }

    public void setAlumnRequirementService(IAlumnRequirementService alumnRequirementService) {
        this.alumnRequirementService = alumnRequirementService;
    }
}
